Skip to content

Fix prefer_self_in_static_references for shadowed nested types#6552

Merged
SimplyDanny merged 3 commits intorealm:mainfrom
theamodhshetty:codex/fix-prefer-self-shadowed-inner-type
Apr 19, 2026
Merged

Fix prefer_self_in_static_references for shadowed nested types#6552
SimplyDanny merged 3 commits intorealm:mainfrom
theamodhshetty:codex/fix-prefer-self-shadowed-inner-type

Conversation

@theamodhshetty
Copy link
Copy Markdown
Contributor

Summary

  • skip prefer_self_in_static_references violations when a nested type shadows its enclosing type name
  • add a rule example covering the shadowed nested type case
  • add the changelog entry

Testing

  • swift test --filter PreferSelfInStaticReferencesRuleGeneratedTests
  • ./.build/debug/swiftlint lint Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfInStaticReferencesRule.swift Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfInStaticReferencesRuleExamples.swift CHANGELOG.md --strict
  • manual repro with only_rules: [prefer_self_in_static_references] against the struct S1 { struct S1 {}; var s = S1() } sample

@SwiftLintBot
Copy link
Copy Markdown

SwiftLintBot commented Mar 25, 2026

21 Messages
📖 Building this branch resulted in a binary size of 27602.27 KiB vs 27601.87 KiB when built on main (0% larger).
📖 Linting Aerial with this PR took 0.81 s vs 0.81 s on main (0% slower).
📖 Linting Alamofire with this PR took 1.02 s vs 1.06 s on main (3% faster).
📖 Linting Brave with this PR took 7.25 s vs 7.22 s on main (0% slower).
📖 Linting DuckDuckGo with this PR took 28.21 s vs 28.23 s on main (0% faster).
📖 Linting Firefox with this PR took 12.21 s vs 12.2 s on main (0% slower).
📖 Linting Kickstarter with this PR took 8.39 s vs 8.38 s on main (0% slower).
📖 Linting Moya with this PR took 0.46 s vs 0.48 s on main (4% faster).
📖 Linting NetNewsWire with this PR took 2.63 s vs 2.61 s on main (0% slower).
📖 Linting Nimble with this PR took 0.71 s vs 0.68 s on main (4% slower).
📖 Linting PocketCasts with this PR took 8.02 s vs 7.79 s on main (2% slower).
📖 Linting Quick with this PR took 0.39 s vs 0.41 s on main (4% faster).
📖 Linting Realm with this PR took 2.9 s vs 2.99 s on main (3% faster).
📖 Linting Sourcery with this PR took 1.89 s vs 1.86 s on main (1% slower).
📖 Linting Swift with this PR took 4.73 s vs 4.7 s on main (0% slower).
📖 Linting SwiftLintPerformanceTests with this PR took 0.34 s vs 0.33 s on main (3% slower).
📖 Linting VLC with this PR took 1.27 s vs 1.26 s on main (0% slower).
📖 Linting Wire with this PR took 18.91 s vs 18.91 s on main (0% slower).
📖 Linting WordPress with this PR took 12.78 s vs 12.75 s on main (0% slower).
📖 This PR fixed a violation in Swift: /stdlib/public/Differentiation/OptionalDifferentiation.swift:53:42: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
📖 This PR fixed a violation in WordPress: /Modules/Sources/AsyncImageKit/ImageDownloader.swift:260:32: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)

Generated by 🚫 Danger

Copy link
Copy Markdown
Collaborator

@SimplyDanny SimplyDanny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! Thank you.

Comment thread Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfInStaticReferencesRule.swift Outdated
Comment thread Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfInStaticReferencesRule.swift Outdated
Comment thread Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfInStaticReferencesRule.swift Outdated
Comment thread Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfInStaticReferencesRule.swift Outdated
@theamodhshetty
Copy link
Copy Markdown
Contributor Author

Thanks for the review. I pushed fc95301 with the requested cleanup.

What changed:

  • removed the separate shadowing stack and folded the skip behavior into parentDeclScopes
  • derived the checked name from ParentDeclBehavior
  • included type aliases in the nested-name check
  • added the extra non-triggering case where the reference appears before the nested type declaration

Validation rerun:

  • swift test --filter PreferSelfInStaticReferencesRuleGeneratedTests
  • ./.build/debug/swiftlint lint Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfInStaticReferencesRule.swift Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfInStaticReferencesRuleExamples.swift --strict

@SimplyDanny SimplyDanny force-pushed the codex/fix-prefer-self-shadowed-inner-type branch from bbee376 to 29eaa45 Compare April 19, 2026 11:35
@SimplyDanny SimplyDanny enabled auto-merge (squash) April 19, 2026 11:36
@SimplyDanny SimplyDanny merged commit ef026db into realm:main Apr 19, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False positive for prefer_self_in_static_references when an inner struct has the same name as its owner

3 participants